# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer:
pkgname=dhcp
pkgver=4.4.3_p1
_projver=${pkgver/_p/-P}
pkgrel=4
pkgdesc="ISC Dynamic Host Configuration Protocol (DHCP)"
url="https://www.isc.org/"
arch="all"
license="MPL-2.0"
depends_static="$pkgname-dev=$pkgver-r$pkgrel"
# busybox utils are not sufficient
_depends_dhclient="
	coreutils
	iproute2
	run-parts
	"
makedepends="
	krb5-dev
	linux-headers
	openldap-dev
	perl
	$_depends_dhclient
	"
pkgusers="dhcp"
pkggroups="dhcp"
install="$pkgname.pre-install"
subpackages="
	$pkgname-dbg
	$pkgname-doc
	$pkgname-openrc
	$pkgname-libs-static
	$pkgname-dev
	$pkgname-server-vanilla:_server_vanilla
	$pkgname-server-ldap:_server_ldap
	dhcrelay
	dhcrelay-openrc
	dhclient
	keama
	"
source="https://downloads.isc.org/isc/dhcp/$_projver/dhcp-$_projver.tar.gz
	fix-unwind-import.patch
	dhcp-3.0-fix-perms.patch
	dhclient-script-alpine
	dhcrelay.initd
	dhcrelay.confd
	dhcpd.confd
	dhcpd.initd
	"
builddir="$srcdir/$pkgname-$_projver"

# secfixes:
#   4.4.3_p1-r0:
#     - CVE-2022-2928
#     - CVE-2022-2929
#   4.4.2_p1-r0:
#     - CVE-2021-25217
#   4.4.1-r0:
#     - CVE-2019-6470
#     - CVE-2018-5732
#     - CVE-2018-5733

_configure() {
	# fix ipv6
	export CFLAGS="$CFLAGS -D_GNU_SOURCE \
		-D_PATH_DHCLIENT_CONF='\"/etc/dhcp/dhclient.conf\"'"

	./configure \
		--build="$CBUILD" \
		--host="$CHOST" \
		--prefix=/usr \
		--sysconfdir=/etc/dhcp \
		--localstatedir=/var \
		--with-srv-conf-file=/etc/dhcp/dhcpd.conf \
		--with-srv-lease-file=/var/lib/dhcp/dhcpd.leases \
		--with-srv6-lease-file=/var/lib/dhcp/dhcpd6.leases \
		--with-cli-lease-file=/var/lib/dhcp/dhclient.leases \
		--with-cli6-lease-file=/var/lib/dhcp/dhclient6.leases \
		--with-srv-pid-file=/run/dhcp/dhcpd.pid \
		--with-srv6-pid-file=/run/dhcp/dhcpd6.pid \
		--with-cli-pid-file=/run/dhcp/dhclient.pid \
		--with-cli6-pid-file=/run/dhcp/dhclient6.pid \
		--with-relay-pid-file=/run/dhcp/dhcrelay.pid \
		--with-relay6-pid-file=/run/dhcp/dhcrelay6.pid \
		--with-libbind=no \
		--enable-dhcpv4o6 \
		--enable-paranoia \
		--enable-log-pid \
		"$@"
}

prepare() {
	# Normally the upstream build system unpacks this before building the
	# library but we need to patch it for segfualt-fix.patch. If we unpack
	# it early then the build system will just use what we've already
	# unpacked and patched.
	tar -C "$builddir"/bind/ -xzf "$builddir"/bind/bind.tar.gz

	default_prepare

	update_config_guess
	update_config_sub
	cp config.guess config.sub bind/bind-*/

	cp -ar "$builddir" "$builddir-ldap"
}

build() {
	_configure
	make -j1 -C bind && make
	make -C keama

	cd "$builddir-ldap"
	_configure \
		--with-ldap \
		--with-ldapcrypto \
		--with-ldap-gssapi
	make -j1 -C bind && make
}

check() {
	make check

	cd "$builddir-ldap"
	make check
}

package() {
	make DESTDIR="$pkgdir" install
	make DESTDIR="$pkgdir" -C keama install

	install -m751 -D "$srcdir"/dhclient-script-alpine "$pkgdir"/sbin/dhclient-script
	install -d -o dhcp -g dhcp "$pkgdir"/var/lib/dhcp
}

openrc() {
	local name="${subpkgname%-openrc}"
	[ "$name" = dhcp ] && name="dhcpd"

	install -m755 -D "$srcdir"/$name.initd "$pkgdir"/etc/init.d/$name
	install -m644 -D "$srcdir"/$name.confd "$pkgdir"/etc/conf.d/$name

	default_openrc
}

static() {
	depends="$depends_static"
	default_static || mkdir -p "$subpkgdir"
}

_server_vanilla() {
	pkgdesc="ISC DHCP server"
	depends="$pkgname=$pkgver-r$pkgrel"
	install_if="$pkgname=$pkgver-r$pkgrel"
	provides="dhcp-server=$pkgver-r$pkgrel"
	provider_priority=100  # highest

	amove usr/sbin/dhcpd
}

_server_ldap() {
	pkgdesc="ISC DHCP server with LDAP support"
	depends="$pkgname=$pkgver-r$pkgrel"
	provides="dhcp-server=$pkgver-r$pkgrel"
	provider_priority=10  # lowest

	install -d "$subpkgdir"
	make DESTDIR="$subpkgdir" -C "$builddir-ldap/server" install-sbinPROGRAMS
}

dhcrelay() {
	pkgdesc="ISC DHCP relay agent"
	depends=""
	install="$subpkgname.pre-install"

	amove usr/sbin/dhcrelay
}

dhclient() {
	pkgdesc="ISC DHCP client"
	depends="$_depends_dhclient"

	amove etc/dhcp/dhclient.conf*
	amove usr/sbin/dhclient
	amove sbin/dhclient-script
	ln -s ../sbin/dhclient-script "$subpkgdir"/etc/dhclient-script

	install -d -o dhcp -g dhcp "$subpkgdir"/var/lib/dhcp
}

keama() {
	pkgdesc="KEA Migration Assistant to translate ISC DHCP configurations to Kea"

	amove usr/sbin/keama
}

# gpg_signature_extensions="sha512.asc"
# gpgfingerprints="
# 	good:AE3F AC79 6711 EC59 FC00  7AA4 74BB 6B9A 4CBB 3D38
# 	BE0E 9748 B718 253A 28BB  89FF F1B1 1BF0 5CF0 2E57
# 	"

sha512sums="
d14dc44d1c015780ae19769816cb01015959927a1ad7a3e84b89e0463253aaf46451af88e3260347196373906d5b438c7c616fee45ec3f128aa82af6702b7154  dhcp-4.4.3-P1.tar.gz
23ab581d85ba97a37fd6a0a612e0aa977b24bbaf83d58a93d1a87f9f24ea9a098aa549e77a6e1d78f721681c152464b15fd1d402d0673edf4dac6aa196df1fe9  fix-unwind-import.patch
d5697a56fbbff25199962608986e7ffb533ed4afd3e344e3c79d2010dda73cc0b088f06c454e9f0c69eb054e09a374455fa71d3f73306e0c98fa76df4dd321b7  dhcp-3.0-fix-perms.patch
d1dce58875793316761f168e29feddc1d3454d1d917d063d43ae102b7b6aab256c3cb420478335c57ebcdb2b7c804afa4d8a1f9ab06a29a4dd23bc5d87db8df2  dhclient-script-alpine
e672ec030137e870afb817ec0fe4e01a7553f331181e481cba1f032f1e18798dd153bf30105ca8361284e6e11f80435ef7c960b0a11c32c5766c30a0c960550c  dhcrelay.initd
970ee381dd6a7e0e30d71e28597d17074421ac167b233f597a129ed9b977cba1026375a020dff2613c3f4d458b71ed9fe4447abfcf5edf27b395e6f2b00f0982  dhcrelay.confd
7b7a77b7826b475a4113ebeee54501ce417cc56e85754301a82a185d88b4713d198f615a366e63e0e2b0aef988c8137dcd1e18c4036d993378257079da17693d  dhcpd.confd
1f78c4f64a891012b4de029c50934b284ad80313959e06b0f29416f33a9e4f0deca8b61e9224a683a92faf8a08e8b7c075bc6e833ab73b6305c0d6d9712d0baf  dhcpd.initd
"
